using AutoMapper;
using MediatR;
using S.O.Domain.RBAC;
using S.O.Domain.Dto.Output;
using S.O.ErrorCode;
using S.O.Interstructrue.Base;
using S.O.Api.Read.Application.Command.Permission;

namespace S.O.Api.Read.Application.Handler.Permission
{
    /// <summary>
    /// 获取权限列表处理器
    /// 负责处理权限列表查询的业务逻辑，支持分页和权限名称模糊搜索
    /// </summary>
    public class GetPermissionListHandler : IRequestHandler<GetPermissionListCommand, APIPaging<PermissionDto>>
    {
        /// <summary>
        /// 权限仓储接口，用于权限数据的查询操作
        /// </summary>
        private readonly IBaseRepository<PermissionModel> permissionRepository;
        
        /// <summary>
        /// 对象映射器，用于Model对象到DTO对象的转换
        /// </summary>
        private readonly IMapper mapper;
        
        /// <summary>
        /// 日志记录器，用于记录操作日志和错误信息
        /// </summary>
        private readonly ILogger<GetPermissionListHandler> logger;

        /// <summary>
        /// 构造函数，通过依赖注入初始化所需的服务
        /// </summary>
        /// <param name="permissionRepository">权限仓储服务</param>
        /// <param name="mapper">对象映射服务</param>
        /// <param name="logger">日志记录服务</param>
        public GetPermissionListHandler(IBaseRepository<PermissionModel> permissionRepository, IMapper mapper, ILogger<GetPermissionListHandler> logger)
        {
            this.permissionRepository = permissionRepository;
            this.mapper = mapper;
            this.logger = logger;
        }
        
        /// <summary>
        /// 处理获取权限列表命令
        /// 执行权限列表查询操作，支持按权限名称模糊搜索和分页
        /// </summary>
        /// <param name="request">获取权限列表命令对象，包含分页参数和搜索条件</param>
        /// <param name="cancellationToken">取消令牌，用于取消异步操作</param>
        /// <returns>返回包含分页权限列表数据的APIPaging对象</returns>
        public Task<APIPaging<PermissionDto>> Handle(GetPermissionListCommand request, CancellationToken cancellationToken)
        {
            // 初始化分页返回结果对象
            APIPaging<PermissionDto> res = new APIPaging<PermissionDto>();
            
            // 获取所有未删除的权限数据
            var permission = permissionRepository.GetALL().Where(x => x.IsDeleted == false);
            
            // 如果提供了权限名称搜索条件，则进行模糊匹配过滤
            if(request.PermissionName != null)
            {
                permission = permission.Where(x => x.PermissionName.Contains(request.PermissionName));
            }
            
            // 执行分页处理，按ID降序排列，根据页码和页大小获取当前页的数据
            var data = permission.OrderByDescending(x => x.Id)
                                .Skip((request.PageIndex - 1) * request.PageSize)
                                .Take(request.PageSize);
            
            // 使用AutoMapper将权限实体列表映射为DTO列表
            res.PageDatas = mapper.Map<List<PermissionDto>>(data);
            
            // 设置总记录数
            res.TotalCount = permission.Count();
            
            // 设置返回结果
            res.Code = APIEnums.Success;
            res.Msg = "获取权限列表成功";
            
            // 记录操作日志
            logger.LogInformation($"获取权限列表成功，共{res.TotalCount}条记录，当前页{request.PageIndex}，页大小{request.PageSize}");
            
            return Task.FromResult(res);
        }
    }
}
